/* 
原型式继承：
利用一个空对象作为中介，将某个对象直接赋值给空对象构造函数的原型
*/
function object (obj) {
    function F(){}
    F.prototype = obj
    return new F()
}
// object()对传入对象进行一次浅复制，将构造函数F的原型直接指向传入的对象，这样子类实例将共享该对象的所有属性和方法
/* let person = {
    name:'person',
    friends:['a','b']
}

let firstP = object(person)
firstP.name = 'xiaoxiao'
firstP.friends.push('cc')
console.log(firstP);

let secondP = object(person)
secondP.name = 'minghua'
secondP.friends.push('dd')
console.log(secondP);
console.log(person); */

// 寄生式继承：在原型式继承基础上，增加对象，返回构造函数
function commonP (original) {
    let clone = object(original) // 通过调用 object() 函数创建一个新对象
    clone.sayFun = function(){   // 以某种方式来增强对象
        console.log('hi hello world');
    }
    return clone  // 返回这个对象
}
let children = {
    name:'children',
    friends:['q1','q2']
}
let anotherP = commonP(children)
anotherP.name = 'xx'
anotherP.friends.push('p1') 
console.log(anotherP);
console.log(anotherP.friends);
anotherP.sayFun()
let otherP = commonP(children)
otherP.name = 'gg'
otherP.friends.push('mm')
console.log(otherP);
console.log(otherP.friends);
otherP.sayFun()
console.log(children);
/*
friends: (4) ["q1", "q2", "p1", "mm"]
name: "children" 
*/
